package org.kfive.nova.io.nio;

import java.io.FileNotFoundException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

/*
* FileMapping.java
* Created  by  lizhanwu
* Created on 2016-5-19 15:17
*/
public class FileMapping {

    public static void main(String[] args) {
        try {
            RandomAccessFile file = new RandomAccessFile("D:\\tmp\\datatype.xml","rw");
            FileChannel channel = file.getChannel();
            MappedByteBuffer byteBuffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, file.length());
            while (true){
                System.out.println("判断是否Loaded:"+byteBuffer.isLoaded());
                if(!byteBuffer.isLoaded()){
                    System.out.println("未Loaded,进行load");
                    byteBuffer.load();
                }
                byte [] data = new byte[1024];
//                while (byteBuffer.get())

                System.out.println(new String(data));
                byteBuffer.put(0,(byte)'a');
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
    }
}
